package com.gupao.pattern.singleton.regiter;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * spring 中的做法，就是用这种注册式的
 */
public class BeanFactory {

    private BeanFactory (){}

    private static Map<String ,Object>  ioc = new ConcurrentHashMap<String, Object>();


    public static  Object getBean(String className) {

        Object obj = null;

        if (!ioc.containsKey(className)){
             try{

                obj = Class.forName(className).newInstance();

                ioc.put(className,obj);

            }catch (Exception e) {
                e.printStackTrace();
            }

            return obj;
        }else {

            return ioc.get(className);
        }
    }
}
